package com.augurit.gzcgwsp.ywcmn.web;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxTwoService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxTwoForm;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "yw-com-qyxx-two.action", type = "redirect"),
    @Result(name = "ReloadSelect", location = "yw-com-qyxx-two!select.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/gzcgwsp/ywcmn/YwComQyxx/YwComQyxx.jsp"),
	@Result(name = CrudActionSupport.INPUT, location = "/gzcgwsp/ywcmn/YwComQyxx/YwComQyxx-input.jsp"),
    @Result(name = "select", location = "/gzcgwsp/ywcmn/YwComQyxx/YwComQyxxSelect.jsp")
})
public class YwComQyxxTwoAction extends CrudActionSupport<YwComQyxxTwoForm>{

	@Autowired
	private IYwComQyxxTwoService ywComQyxxTwoService;

	private YwComQyxxTwoForm form;
	private Long id;
	private Long[] checkedIds;
	private Page<YwComQyxxTwoForm> page = new Page<YwComQyxxTwoForm>(10);


	@Override
	public String list() throws Exception {
		ywComQyxxTwoService.search(page, form);
		return SUCCESS;
	}
	
	public String dataList() throws Exception {
        ExtUtils.initPageFromExtGridParam(getRequest(), page);
        ywComQyxxTwoService.search(page, form);
        renderText(Struts2Utils.getJsonString(page, DateUtils.YEAR_MOUTH_DAY_));
        return null;
	}
	
    public String select() throws Exception {
        ywComQyxxTwoService.search(page, form);
        super.getRequest().setAttribute("flag", "select");
        return "select";
    }

	@Override
	public String input() throws Exception {
        String flag = (String) super.getRequest().getAttribute("flag");
        super.getRequest().setAttribute("flag", flag);
		return INPUT;
	}

	@Override
	public String save() throws Exception {
        ywComQyxxTwoService.save(form);
        
	    String flag = (String) super.getRequest().getAttribute("flag");
	    if (StringUtils.isNotBlank(flag) && "select".equals(flag)) {
	        return "ReloadSelect";
	    }
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		ywComQyxxTwoService.delete(id);
        
        String flag = (String) super.getRequest().getAttribute("flag");
        if (StringUtils.isNotBlank(flag) && "select".equals(flag)) {
            return "ReloadSelect";
        }
		return RELOAD;
	}
	
	@Override
	public String deleteMore() throws Exception{
		ywComQyxxTwoService.delete(checkedIds);
		
        String flag = (String) super.getRequest().getAttribute("flag");
        if (StringUtils.isNotBlank(flag) && "select".equals(flag)) {
            return "ReloadSelect";
        }
		return RELOAD;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = ywComQyxxTwoService.get(id);
		}
		else
			form = new YwComQyxxTwoForm();
	}
	

	public YwComQyxxTwoForm getModel() {
		return form;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<YwComQyxxTwoForm> getPage() {
		return page;
	}


	public void setPage(Page<YwComQyxxTwoForm> page) {
		this.page = page;
	}
}
